package com.java.tutorials.spring.applicationlistener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
//import org.springframework.scheduling.quartz

import java.util.TimerTask;
/**
 * Created by xuzhike on 2016/1/30.
 * http://www.java2s.com/Code/Java/Spring/ImplementsApplicationListener.htm
 * Note: org.springframework.scheduling.timer.ScheduledTimerTask 已经废弃
 *
 */
public class ImplementsApplicationListener {
}

class HeartbeatTask extends TimerTask implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher eventPublisher;

    public void run() {
        HeartbeatEvent event = new HeartbeatEvent(this);
        eventPublisher.publishEvent(event);
    }

    public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }
}

class HeartbeatForwarder implements ApplicationListener {
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof HeartbeatEvent) {
            System.out.println("Received heartbeat event: " + event.getTimestamp());
        }
    }
}

class HeartbeatEvent extends ApplicationEvent {
    public HeartbeatEvent(Object source) {
        super(source);
    }
}


